added Feb 2001 SDK
[windows-sources.git] / shared source / sscli20 / samples / compilers / myc / myc.html
blobdce4d1d565ad5735e5ac22e9d05bd2945c3e2116
1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2 <html>
3 <head>
4 <title>MyC Compiler Sample</title>
5 <link rel="stylesheet" type="text/css" href="../../../docs/rotor.css">
6 </head>
8 <body>
11 <h1>MyC Compiler Sample</h1>
14 <h2>Sample Overview</h2>
17 <p>The goal of the MyC compiler is to show the implementation of various
18 features of the CLI. </p>
21 <p>The following table shows the command-line options for this sample.</p>
23 <table border="1" width="90%">
24 <tr>
25 <th>Option</th>
26 <th>Description</th>
27 <tr>
28 <td><b>/debug </b>
29 <td>Generates debug information.<tr>
30 <td><b>/nodebug </b>
31 <td>Prevents generation of debug information.<tr>
32 <td><b>/list </b>
33 <td>Generates an assembly listing (.asm).<tr>
34 <td><b>/dll </b>
35 <td>Creates a DLL assembly.
36 <tr>
37 <td><b>/exe </b>
38 <td>Creates an executable assembly.
39 <tr>
40 <td><b>/outdir:path </b>
41 <td>Creates output files in the current directory.</tr>
42 </table>
45 <h3>Architecture Diagram</h3>
48 <p align="center"><img border="1" src="mycarch.gif" width="372" height="733"></p>
51 <p align="left">&nbsp;</p>
54 <h3>MyC Language Specification</h3>
55 <h4>Goals</h4>
56 <p>The goal of the MYC compiler is to show common intermediate language (CIL)
57 instructions and the CLI in action. </p>
58 <h4>Overview </h4>
59 <p>The compiler itself is a simple recursive descent parser with a single pass
60 code generator. It generates an assembler source file which is then used as
61 input to the IL Assembler. </p>
62 <h4>Language Specification </h4>
63 <p>The language is a subset of the C language with
64 simplified declarations, both external and local. </p>
65 <h4>Data Types </h4>
66 <p>The only supported data types are <b>int</b> and <b>void</b>. Limiting
67 these choices allows a simpler sample compiler design. </p>
68 <h4>Data Declaration </h4>
69 <p>Variables can be declared static or local. Implicit
71 static declarations occur outside of function declarations. Local declarations
72 can occur only at the beginnings of functions prior to statements. </p>
73 <p>For example: </p>
74 <PRE>int func1()
76 int y;
78 int x;
80 int func2()
82 static int z;
84 </PRE>
85 <P>
86 In this example the variable <i>x</i> is a static declaration (by default) and the
87 variable <i>y</i> is a local variable in function <b>func1()</b>. The variable
89 <i>z</i> is actually a static, even though it is declared within a function.
90 <h4>Flow Control </h4>
91 <UL>
92 <LI><b>if-else</b> blocks
93 <LI><b>while</b> loops
94 <LI><b>for</b> loops </LI>
95 </UL>
96 <h4>Restrictions </h4>
97 <UL>
98 <LI>Integer data
99 <LI>Outer declarations only
100 <LI>Static methods and function calls only </LI>
101 </UL>
102 <h3>Backus-Naur Definition</h3>
105 <p>letter ::= &quot;A-Za-z&quot;;<br>
106 digit ::= &quot;0-9&quot;;<br>
107 <br>
108 name ::= letter { letter | digit };<br>
109 integer ::= digit { digit };<br>
110 <br>
111 ident ::= name | function_call;<br>
112 function_call ::= name &quot;(&quot; [expr {, expr}] &quot;)&quot;;<br>
113 <br>
114 factor ::= (ident | integer | &quot;(&quot; expr &quot;)&quot; );<br>
115 unary_factor ::= [&quot;+&quot;|&quot;-&quot;] factor;<br>
116 <br>
117 term1 ::= [&quot;*&quot;|&quot;/&quot;] factor;<br>
118 term0 ::= factor { term1 };<br>
119 first_term ::= unary_factor term1;<br>
120 <br>
121 math_expr ::= first_term { [&quot;+&quot;|&quot;-&quot;] term0 }<br>
122 rel_expr ::= math_expr (&quot;==&quot;|&quot;!=&quot;|&quot;&lt;&quot;|&quot;&gt;&quot;|&quot;&gt;=&quot;|&quot;&lt;=&quot;) math_expr;<br>
123 not_factor ::= [&quot;!&quot;] rel_expr;<br>
124 term_bool ::= not_factor { (&quot;&amp;&quot; | &quot;&amp;&amp;&quot;) not_factor };<br>
125 bool_expr ::= term_bool { (&quot;|&quot; | &quot;^&quot;) term_bool };<br>
126 expr ::= bool_expr;<br>
127 <br>
128 assign = ident &quot;=&quot; expr;<br>
129 assign_stmt ::= assign &quot;;&quot; ;<br>
130 if_stmt ::= &quot;if&quot; &quot;(&quot; expr &quot;)&quot; stmt_block [ &quot;else&quot; inner_block ];<br>
131 while_stmt ::= &quot;while&quot; &quot;(&quot; expr &quot;)&quot; inner_block;<br>
132 for_stmt ::= &quot;for&quot; &quot;(&quot; assign &quot;;&quot; expr &quot;;&quot; assign &quot;)&quot; inner_block<br>
133 break_stmt ::= &quot;break&quot; &quot;;&quot;;<br>
134 cont_stmt ::= &quot;continue&quot; &quot;;&quot;;<br>
135 ret_stmt ::= &quot;return&quot; expr &quot;;&quot;;<br>
136 stmt ::= (<br>
137 if_stmt<br>
138 | while_stmt<br>
139 | for_stmt<br>
140 | break_stmt<br>
141 | cont_stmt<br>
142 | ret_stmt<br>
143 | assign_stmt<br>
144 );<br>
145 inner_block ::= &quot;{&quot; { stmt } &quot;}&quot;;<br>
146 outer_block ::= &quot;{&quot; { inner_decl } { stmt } &quot;}&quot;;<br>
147 <br>
148 inner_decl ::= [ class ] type ident { &quot;,&quot; ident } &quot;;&quot;;<br>
149 <br>
150 class ::= &quot;extern&quot; | &quot;static&quot; | &quot;auto&quot;;<br>
151 type ::= &quot;int&quot; | &quot;void&quot;;<br>
152 <br>
153 params ::= type ident { , type ident };<br>
154 outer_decl ::= [ class ] type ident { &quot;,&quot; ident } &quot;;&quot;;<br>
155 func_decl ::= [ class ] type ident &quot;(&quot; params &quot;)&quot; outer_block;<h2>Sample Source and Build Output Locations</h2>
158 <p>The sample source is found in sscli20\samples\compilers\myc\src directory.&nbsp; The source
159 files are:</p>
162 <ul class="none">
163 <li><a href="src/asm.cs">asm.cs</a></li>
164 <li><a href="src/emit.cs">emit.cs</a></li>
165 <li><a href="src/exe.cs">exe.cs</a></li>
166 <li><a href="src/iasm.cs">iasm.cs</a></li>
167 <li><a href="src/io.cs">io.cs</a></li>
168 <li><a href="src/myc.cs">myc.cs</a></li>
169 <li><a href="src/parse.cs">parse.cs</a></li>
170 <li><a href="src/tok.cs">tok.cs</a></li>
171 <li><a href="src/var.cs">var.cs</a></li>
172 <li><a href="src/varlist.cs">varlist.cs</a></li>
173 </ul>
176 <p>The build output location is %_NTTREE%\samples\compilers\myc.&nbsp;
177 The output file is an executable assembly named myc.exe.</p>
180 <h2>Building the Sample</h2>
183 <p>All samples are built from the buildall script.&nbsp; </p>
186 <p>You can also build all the
187 samples by switching to the root of the sample directory, sscli20\samples, and typing
188 <code>build -c</code>.</p>
191 <p>You can build this specific sample by switching to the sample directory and typing
192 <code>build -c</code>.</p>
195 <h2>Running the Sample</h2>
198 <p>Because the MyC language does not support referencing external assemblies,
199 use the Runtime Debugger to step through test applications built using MyC.</p>
202 <ol>
203 <li>Run env.bat. </li>
204 <li>Build the SSCLI using the buildall script or batch file.</li>
205 <li>Switch to the %_NTTREE%\samples\compilers\myc directory.</li>
206 <li>Type the following command:<blockquote>
209 <p>clix myc /debug <i>inputfile</i></p>
212 </blockquote>
215 <p>where <i>inputfile</i> is the name of a MyC source file.&nbsp; For example:</p>
218 <blockquote>
221 <p>clix myc /debug tflow.myc</p>
224 </blockquote>
225 </li>
226 <li>Debug the resulting assembly:</li>
227 </ol>
228 <blockquote>
229 <blockquote>
232 <p>cordbg tflow.exe</p>
235 </blockquote>
236 </blockquote>
237 <h2>
238 Known Issues </h2>
241 <p>The debug line number count is off by 1.</p>
244 <h2>References</h2>
245 <ul>
246 <li><i>Modern Compiler Implementation in C</i>, by Andrew W Appel.</li>
247 <li><i>Algorithms in C++</i> by Robert Sedgewick.</li>
248 <li><i>Advanced Compiler Design and Implementations</i>, by Steven S Muchnick.</li>
249 <li><i>Compiler Construction</i>, by Niklaus Wirth.</li>
250 <li><i>Principles of Compiler Design</i>, by Alfred V. Aho and and Jeffrey D. Ullman.</li>
251 </ul>
257 <p><i>Copyright (c) 2006 Microsoft Corporation. All rights reserved.</i></p>
258 </body>
259 </html>